package cn.icatw.leetcode.editor.cn;
//给定两个整数数组 inorder 和 postorder ，其中 inorder 是二叉树的中序遍历， postorder 是同一棵树的后序遍历，请你构造并
//返回这颗 二叉树 。
//
//
//
// 示例 1:
//
//
//输入：inorder = [9,3,15,20,7], postorder = [9,15,7,20,3]
//输出：[3,9,20,null,null,15,7]
//
//
// 示例 2:
//
//
//输入：inorder = [-1], postorder = [-1]
//输出：[-1]
//
//
//
//
// 提示:
//
//
// 1 <= inorder.length <= 3000
// postorder.length == inorder.length
// -3000 <= inorder[i], postorder[i] <= 3000
// inorder 和 postorder 都由 不同 的值组成
// postorder 中每一个值都在 inorder 中
// inorder 保证是树的中序遍历
// postorder 保证是树的后序遍历
//
//
// Related Topics 树 数组 哈希表 分治 二叉树 👍 1238 👎 0


import java.util.HashMap;

//Java：从中序与后序遍历序列构造二叉树
public class T106_ConstructBinaryTreeFromInorderAndPostorderTraversal {
    public static void main(String[] args) {
        Solution solution = new T106_ConstructBinaryTreeFromInorderAndPostorderTraversal().new Solution();
        // TO TEST
    }
    //leetcode submit region begin(Prohibit modification and deletion)

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     * int val;
     * TreeNode left;
     * TreeNode right;
     * TreeNode() {}
     * TreeNode(int val) { this.val = val; }
     * TreeNode(int val, TreeNode left, TreeNode right) {
     * this.val = val;
     * this.left = left;
     * this.right = right;
     * }
     * }
     */
    class Solution {
        private int postIndex;
        private int[] inorder;
        private int[] postorder;
        private HashMap<Integer, Integer> inorderIndexMap;

        public TreeNode buildTree(int[] inorder, int[] postorder) {
            this.inorder = inorder;
            this.postorder = postorder;
            this.postIndex = postorder.length - 1;
            this.inorderIndexMap = new HashMap<>();
            //    将中序遍历的值和索引存入map
            for (int i = 0; i < inorder.length; i++) {
                inorderIndexMap.put(inorder[i], i);
            }
            return buildTree(0, inorder.length - 1);
        }

        private TreeNode buildTree(int left, int right) {
            //如果没有节点构造子树
            if (left > right) {
                return null;
            }
            //    选择后序遍历的最后一个节点作为根节点
            int rootVal = postorder[postIndex--];
            //    构造根节点
            TreeNode root = new TreeNode(rootVal);
            //    根据root在中序遍历的位置分成左右两颗子树
            int rootIndex = inorderIndexMap.get(rootVal);
            //    构造右子树
            root.right = buildTree(rootIndex + 1, right);
            //    构造左子树
            root.left = buildTree(left, rootIndex - 1);
            return root;
        }
    }

    //leetcode submit region end(Prohibit modification and deletion)
    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}
